package arrarylist;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-04-25
 * Time: 10:55
 */
public class MyArraryList {
    private int max = 5;
    //创建数组
    private  int [] array;
    //记录数量
    private  int size;

    public MyArraryList() {
        this.array =  new int[max];
        this.size = 0;
    }

    //获取数组的长度
    public  int length() {
        return size;
    }

    //获取数组pos位置的值
    public int getposdata(int pos) {
        return array[pos];
    }


    //打印数组
    public void display() {
        for (int i = 0; i < size; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
    //判断数组是否满了
    public boolean isfull() {
       return size == max;
    }

    //判断数组是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    //判断位置是否合法
    public boolean isleage(int pos) {
        if( pos < 0 || pos >size) {
            return false;
        }
        return true;
    }

    //往数组尾部添加数据
    public void addRear(int data) {
        if( isfull()) {
            //扩容
            array = Arrays.copyOf( array,2*size);
            max *= 2;
        }
        //添加数据
        array[size++] = data;
    }

    //往数组指定位置插入数据
    public void addpos(int pos,int data) {
        if(! isleage(pos) ) {
            throw new ArrayListisleageException("输入的位置不合法");
        }
        if(isfull()) {
            array = Arrays.copyOf(array,2*size);
            max *= 2;
        }
        //挪动数据
        for (int i = size-1; i >=pos; i--) {
             array[i+1] = array[i];
        }

        //插入数据
        array[pos] = data;
        size++;
    }


    //修改数组pos位置的值
    public void setposdata(int pos,int data) {
        if(! isleage(pos) ) {
            throw new ArrayListisleageException("输入的位置不合法");
        }
        if( isEmpty() ){
            throw new ArraryListEmptyException("数组为空");
        }
        array[pos] = data;
    }

    //删除指定位置的数据
    public void deletepos(int pos) {
        if(! isleage(pos) ) {
            throw new ArrayListisleageException("输入的位置不合法");
        }
        if( isEmpty() ){
            throw new ArraryListEmptyException("数组为空");
        }
        //覆盖数据
        for (int i = pos; i < size-1; i++) {
            array[i] = array[i+1];
        }
        size--;
    }

    //删除第一次出现的指定的数据
    public void deletedata(int data) {
        if( isEmpty() ){
            throw new ArraryListEmptyException("数组为空");
        }
        for (int i = 0; i < size; i++) {
            if( array[i] == data) {
                //覆盖数据
                for(int j=i; j < size-1 ;j++) {
                    array[j] = array[j+1];
                }
                break;
            }
        }
        size--;
    }

    public void remove(int data) {
        if( isEmpty() ) {
            throw new ArraryListEmptyException("数组为空");
        }
        boolean flag = false;
        for(int i=0;i<size ;i++) {
            if( array[i] == data) {
                flag = true;
                for(int j=i ; j<size-1 ;j++) {
                    array[j] = array[j+1];
                }
                size--;
                break;
            }
        }
        if(flag == true) {
            System.out.println("修改成功");
        } else {
            System.out.println("要删除的数据不存在");
        }
    }
}
